package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ：Shijiu
 * @create  2019/09/10 15:19
 * @description ：菜单的service
 * @version: 1.0
 */

@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    /**
     * 查询一级菜单
     * @return
     */
    @Override
    public List<Menu> findFirstMenus() {
        return menuDao.findFirstMenus();
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Menu> page=menuDao.findByPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page);
    }

    /**
     * 添加菜单
     * @param menu
     */
    @Override
    @Transactional
    public void add(Menu menu) {
        Integer parentMenuId = menu.getParentMenuId();
        if (parentMenuId==null){
            //一级菜单
            menu.setLevel(1);
            //查询一级菜单的路径
            String lastPath=menuDao.findMenuPathByLevelOne();
            int i = Integer.parseInt(lastPath);
            String path=String.valueOf(i+1);
            //设置路径
            menu.setPath(path);
        }else {
            //二级菜单
            menu.setLevel(2);
            //查询一级菜单的路径
            String lastPath=menuDao.findMenuPathByLevelTwo(menu.getParentMenuId());

            if (lastPath==null){
                //是该菜单的第一个子菜单
                //查询父级菜单的路径
                String parentPath=menuDao.findParentPathByParentMenuId(menu.getParentMenuId());
                String path="/"+parentPath+"-"+1;
                menu.setPath(path);
            }else {
                //设置路径
                String[] split = lastPath.split("-");
                int i = Integer.parseInt(split[1]);
                String path=split[0]+"-"+String.valueOf(i+1);
                menu.setPath(path);
            }

        }
        menuDao.add(menu);
    }

    /**
     * 根据id查找菜单
     * @param id
     * @return
     */
    @Override
    public Menu findMenuById(Integer id) {
        return menuDao.findMenuById(id);
    }

    /**
     * 更新
     * @param menu
     */
    @Override
    public void update(Menu menu) {
        menuDao.update(menu);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id) {
        //判断此菜单是否与用户关联
        long count=menuDao.findRoleNumByMenuId(id);
        if (count!=0){
            //有关联
            throw new RuntimeException("该菜单与用户有关联，删除失败");
        }else {
             menuDao.delete(id);
        }
    }

    //查询所有父菜单
    @Override
    public List<Menu> findAllParentMenu() {
        return menuDao.findAllParentMenu();
    }

    //根据主菜单选的的id查询对应的子菜单
    @Override
    public List<Menu> findAllSubMenu() {
        return menuDao.findAllSubMenu();
    }
}
